﻿#include "ui_cmd.h"
#include "ui_ui_cmd.h"
#include <QDebug>
#include <QMessageBox>

uiCmd::uiCmd(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::uiCmd)
{
    ui->setupUi(this);
    ui->bg->setCurrentIndex(0);

    this->m_xml = new ToolXml;
}

uiCmd::~uiCmd()
{
    delete ui;
}

void uiCmd::on_pushButton_clicked()
{
    ui->bg->setCurrentIndex(1);
}

void uiCmd::on_pushButton_3_clicked()
{
    ui->bg->setCurrentIndex(0);
}

void uiCmd::on_pushButton_2_clicked()
{
    qDebug() << "设置cmd";
    bool ok;
    bool ret = true;

    LocalDev local = *this->m_ld;
    localCmd_t cmd;
    cmd.cmd = ui->cmdEdit->text().toUInt(&ok);
    ret = ok ? ret : ok;
    cmd.x = ui->xEdit->text().toFloat(&ok);
    ret = ok ? ret : ok;
    cmd.y = ui->yEdit->text().toFloat(&ok);
    ret = ok ? ret : ok;
    cmd.z = ui->zEdit->text().toFloat(&ok);
    ret = ok ? ret : ok;

    qDebug() << "sn: " << local.getSn();
    qDebug() << "cmd: " << cmd.cmd;
    qDebug() << "x: " << cmd.x;
    qDebug() << "y: " << cmd.y;
    qDebug() << "z: " << cmd.z;

    if (!ret)
    {
        QMessageBox::information(this, "参数错误", "输入的参数有误！\t");
        return;
    }

    local.setCmd(cmd);

    QString xml = m_xml->makeSetCmdXml(&local);

    emit setCmd(xml);
}

void uiCmd::refrCmd(LocalDev *ld)
{
    qDebug() << "refrCmd";

    this->m_ld = ld;
    localCmd_t cmd = ld->getCmd();

    // 初始化ui
    ui->cmd->setText(QString::number(cmd.cmd));
    ui->x->setText(QString::number(cmd.x));
    ui->y->setText(QString::number(cmd.y));
    ui->z->setText(QString::number(cmd.z));

    ui->cmdEdit->setText(QString::number(cmd.cmd));
    ui->xEdit->setText(QString::number(cmd.x));
    ui->yEdit->setText(QString::number(cmd.y));
    ui->zEdit->setText(QString::number(cmd.z));
}
